<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Detail;
use App\Admin\Actions\Grid\Project\Material;
use App\Admin\Repositories\Project;
use App\Models\Category;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class ProjectController extends AdminController
{

    public string $create_permission_name = 'project-create';
    public string $edit_permission_name = 'project-edit';
    public string $delete_permission_name = 'project-delete';
    public string $upload_permission_name = 'project-upload';


    //获取是否拥有上传资料的权限
    public function upload(): bool
    {
        return Admin::user()->isAdministrator() || Admin::user()->can($this->upload_permission_name);
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Project(), function (Grid $grid) {
            $grid->model()->with('category')->withCount('material')->orderBy('sort')->orderBy('date');
            $grid->column('id');
            $grid->column('category.name', trans('project.fields.category_id'));
            $grid->column('title');
            $grid->column('material_count', trans('project.fields.material_count'));
            $grid->column('image')->image('', 100, 100);
            $grid->column('date');
            $grid->column('province_id')->distpicker();
            $grid->column('city_id')->distpicker();
            //$grid->column('district_id')->distpicker();
            $grid->column('price');
            $grid->column('total');
            $grid->column('number');
            if ($this->edit && Admin::user()->can($this->edit_permission_name) && !$this->isTrashed()) {
                $grid->column('show')->switch();
            } else {
                $grid->column('show')->bool();
            }
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('category_id')->select(Category::query()->pluck('name', 'id'));
                $filter->like('title');
            });
            $_this = $this;
            $grid->actions(function (Grid\Displayers\Actions $actions) use ($_this) {
                $actions->append(new Detail());
                if ($_this->upload()) {
                    $actions->append(new Material());
                }
            });
            $this->gridCommon($grid);
            if (!$this->edit || ($this->edit && Admin::user()->cannot($this->edit_permission_name)) || $this->isTrashed()) {
                $grid->disableEditButton();
            }
            $grid->disableQuickEditButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public function detail(int $id): Show
    {
        return Show::make($id, Project::with(['category']), function (Show $show) {
            $show->field('category.name', trans('project.fields.category_id'));
            $show->field('title');
            $show->field('image')->image('', 100, 100);
            $show->field('describe');
            $show->field('date');
            $show->field('province_id')->as(function ($value) {
                return getAreaName($value);
            });
            $show->field('city_id')->as(function ($value) {
                return getAreaName($value);
            });
//            $show->field('district_id')->as(function ($value) {
//                return getAreaName($value);
//            });
            $show->field('price');
            $show->field('total');
            //$show->field('content');
            $show->field('number');
            $show->field('show')->bool();
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        return Form::make(new Project(), function (Form $form) {
            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->select('category_id')->options(Category::query()->where('show', true)->pluck('name', 'id'))->required();
            $form->text('title')->required()->maxLength('100');
            $form->image('image')->move('images/' . date('Y') . '/' . date('m') . '/' . date('d'))->uniqueName()->autoUpload()->required()->removable(false);//->help('推荐尺寸：414*160');
            $form->textarea('describe')->required();
            $form->date('date')->required();
            $form->distpicker(['province_id', 'city_id'])->required();
            $form->number('price')->required()->min(0.01);
            $form->number('total')->default(1)->min(1)->max(99999)->required();
            $form->editor('content')->required();
            $form->switch('show')->default(true);
            $form->number('sort')->default(100)->min(1)->max(100);
        });
    }
}
